﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CommandParameters.h"

#include "CmdShowGcsMainWindow.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "ModuleNames.h"
#include "UIService.h"
#include "StringUtil.h"
#include "IGmepGcsProjectManager.h"
#include "IEditMode.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "GmepPerformanceTimer.h"
#include "GmepComponentUpgradeUtils.h"
#include "GnufMessageBoardInterface.h"
#include "GmepRecordTimeUtils.h"
#include "GapDocumentStatesManager.h"
#include "GmepDocumentStateSetting.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 



using namespace gmepgcs;
using namespace gcmp;
using namespace gmep;

REGISTER_COMMAND(GCS_MODULE_NAME, CmdShowGcsMainWindow)
CREATE_DEBUG_MODE(EnableCheckComponentUpgradeStatus, L"【机电】打开项目构件对话框时开启项目库升级状态查询", DebugModeGroup::DMGT_OTHERS, L"GMEP", L"2024-01-30", L"打开项目构件对话框时开启项目库升级状态查询");


CmdShowGcsMainWindow::CmdShowGcsMainWindow() : gmep::GmepCommandBase(ID_SHOW_GMEPGCS_MAINWINDOW_COMMAND,true)
{
}

CmdShowGcsMainWindow::~CmdShowGcsMainWindow()
{

}

bool CmdShowGcsMainWindow::IsEnabledImpl() const
{
    IUiDocumentViewManager* pManager = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pManager, L"pManager为空", L"GMEP", L"2024-01-30");
    gcmp::IUiDocument* pUIDoc = pManager->GetCurrentUiDocument();
    if (!pUIDoc)
    {
        return false;
    }
    gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();
    if (!pDoc)
    {
        return false;
    }
    if (IEditMode::GetTopActiveEditMode(pDoc))
    {
        return false;
    }
    return  true;
}

bool CmdShowGcsMainWindow::IsVisibleImpl() const
{
    return true;
}

OwnerPtr<IAction> CmdShowGcsMainWindow::ExecuteCommand(const CommandParameters& cmdParams)
{
    GmepRecordTimeUtils::RecordFunctionalServicesStartTime(GMEP_SHOW_PROJECT_COMPONENT_WINDOW);
    IProjectManager* pPrjMgr = IProjectManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPrjMgr, L"pPrjMgr为空", L"GMEP", L"2024-01-30");

    auto projectId = pPrjMgr->GetCurrentProjectId();
    std::wstring forbidOpenMessage = L"";
    // 本地版支持允许projectId 为空
    if (!projectId.empty() && DEBUG_MODE(EnableCheckComponentUpgradeStatus))
    {
        bool isAllowToOpen = false;
        std::wstring upgradeMessage;
        GmepComponentUpgradeUtils::GetIsAllowToOpenByProjectComponentLibUpgradeStatus(projectId, isAllowToOpen, upgradeMessage);
        if (!isAllowToOpen)
        {
            forbidOpenMessage = upgradeMessage;
        }
    }

    if (!forbidOpenMessage.empty())
    {
        gnuf::ShowMessageBoard(forbidOpenMessage, GBMP_TR(L"提示"), gnuf::eMB_OK | gnuf::eMB_ICONINFORMATION);
        return nullptr;
    }

    UIService * pUiservice = UIService::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiservice, L"pUiservice为空", L"GMEP", L"2024-01-30");
    pUiservice->RefreshUIpermission();
    pUiservice->ShowMainWindow();
    pUiservice->SetIsByEditComponentShow(false);

    auto itor = cmdParams.find(ID_CMD_PARAM_GCS_UI_ROUTE_PATH);
    if (itor != cmdParams.end())
    {
        std::wstring path = itor->second.AsWString();//QString::fromStdWString(itor->second.AsWString()).trimmed().toLower();

        path = gcmp::StringUtil::TrimSpace(path);

        if (gcmp::StrICmp(path.c_str(),L"gcs/project"))
        {
            pUiservice->GotoProjectPage();
            return nullptr;
        }
    }

    GmepDocumentStateSetting* pDocStateSettings = GmepDocumentStateSetting::Get();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDocStateSettings, L"pDocStateSettings为空", L"GMEP", L"2024-01-30");
    gap::DocumentStates docState = pDocStateSettings->GetDocumentState();

    bool isCollaborationDocument = docState == gap::DocumentStates::Cloud;

    pUiservice->RefreshCurrentPage(isCollaborationDocument);
    GmepRecordTimeUtils::RecordFunctionalServicesEndTimeAndGetDuration(GBMP_TR(L"构件管理打开"), GBMP_TR(L"构件相关"),
        GMEP_SHOW_PROJECT_COMPONENT_WINDOW);
    return nullptr;
}